﻿//  Copyright © Decebal Mihailescu 2013-2015
//  All rights reserved. This source code and binaries are distributed under
//  Microsoft Public PatternLicense (Ms-PL)
//  The latest code is available at http://VSDesignPatterns.codeplex.com

//  Redistributions of source code must retain the above copyright notice,
//  this list of conditions and the following disclaimer.
//  Redistributions in binary form must reproduce the above copyright notice,
//  this list of conditions and the following disclaimer in the documentation
//  and/or other materials provided with the distribution.

//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
//  REMAINS UNCHANGED.

using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Windows.Forms;

namespace DesignPatterns
{
    using Common;
    using DesignPatternsWizard;
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class AddDesignPatterns
    {
        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 0x0100;
       
        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("96eb8ee9-0372-4020-b9d1-192398882ba3");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="AddDesignPatterns"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private AddDesignPatterns(Package package, LocationsEnum location)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;
            _location = location;
            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {

                var menuCommandID = new CommandID(CommandSet, CommandId);
                //only for VS package
                //var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                //commandService.AddCommand(menuItem);
                DesignPatternsPackage pk = package as DesignPatternsPackage;
                //see http://stackoverflow.com/questions/21929372/dynamic-visibility-of-menu-item
                OleMenuCommand omenuItem = new OleMenuCommand(pk.MenuItemCallback, menuCommandID);//OleMenuCommand extends MenuCommand
                commandService.AddCommand(omenuItem);
                omenuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;
            }
        }
        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package, LocationsEnum location)
        {
            Instance = new AddDesignPatterns(package, location);
        }
        private readonly LocationsEnum _location;
        private void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            OleMenuCommand oMenuCommand = sender as OleMenuCommand;
            DesignPatternsPackage pk = package as DesignPatternsPackage;
            pk.SetMenuAppearance(oMenuCommand, _location);
        }
        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static AddDesignPatterns Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }



        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            string title = "AddDesignPatterns";

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.ServiceProvider,
                message,
                title,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

            //DesignPatternsPackage dpp = this.package as DesignPatternsPackage;
            // int wnd = dpp.Dte.MainWindow.HWnd;
            //MessageBox.Show("This is from Message Box:" + wnd.ToString("X"));
            //EnvDTE80.DTE2 dte = package.GetAsService<EnvDTE80.DTE2>();
            //MessageBox.Show("This is from Message Box dte query :" + ((dte == null)?"NULL":dte.MainWindow.HWnd.ToString("X")));
            IWin32Window w = package.GetAsService<IWin32Window>();
            MessageBox.Show("This is from Message Box  IWin32Window query :" + ((w == null) ? "NULL" : w.Handle.ToString("X")));
        }


    }

}
